const VERSION = 'v0.0.0';

const path = require('path');
const webpack = require('webpack');
const pathConfig = require('./pathConfig');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin')
const modifyVars = require('./modifyVars');
const DEPLOY_TIME = Date.now();

module.exports = {
  mode: 'production',
  entry: {
    app: pathConfig.appIndexJs,
    updating: pathConfig.appUpdating
  },
  output: {
    publicPath: '/',
    path: pathConfig.appBuild,
    filename: 'static/js/[name].[chunkhash:8].js',
    chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js'
  },
  resolve: {
    extensions: ['.js'],
    alias: require('./alias'),
    modules: [pathConfig.appSrc, pathConfig.appNodeModules]
  },

  optimization: {
    minimizer: [
      new TerserJSPlugin({
      terserOptions:{
      sourceMap: false
      }
    })],
  },
  module: {
    rules: [
      {
        oneOf: [
          {
            test: /route\.js$/,
            exclude: /node_modules/,
            use: [
              {
                loader: 'babel-loader',
                options: {
                  cacheDirectory: true,
                  presets: [
                    ['@babel/preset-env', {
                      "targets": "> 0.25%, not dead"
                    }], '@babel/preset-react'],
                  plugins: [
                    [
                      '@babel/plugin-transform-runtime',
                      {
                        helpers: false,
                        regenerator: true,
                        useESModules: true
                      }
                    ],
                    '@babel/plugin-syntax-dynamic-import',
                    ['@babel/plugin-proposal-decorators', { legacy: true }],
                    ['@babel/plugin-proposal-class-properties', { loose: true }],
                    ['import', { libraryName: 'antd', style: true }]
                  ]
                }
              },
              {
                loader: './config/loader/transformer'
              },
              'lodash'
            ]
          },
          {
            test: /\.js$/,
            exclude: /node_modules/,
            use: [
              {
                loader: 'thread-loader',
                options: {
                  workers: 4
                }
              },
              {
                loader: 'babel-loader',
                options: {
                  cacheDirectory: true,
                  presets: ['@babel/preset-env', '@babel/preset-react'],
                  plugins: [
                    ['@babel/plugin-proposal-optional-chaining'],
                    [
                      '@babel/plugin-transform-runtime',
                      {
                        helpers: false,
                        regenerator: true,
                        useESModules: true
                      }
                    ],
                    '@babel/plugin-syntax-dynamic-import',
                    ['@babel/plugin-proposal-decorators', { legacy: true }],
                    ['@babel/plugin-proposal-class-properties', { loose: true }],
                    ['import', { libraryName: 'antd', style: true }]
                  ]
                }
              },
              'lodash'
            ]
          },
          {
            test: /\.(bmp|png|svg|jpg|jpeg|gif)$/,
            use: {
              loader: 'url-loader',
              options: {
                limit: 10000,
                name: 'static/media/[name].[hash:8].[ext]'
              }
            }
          },
          {
            test: /\.css$/,
            use: [
              MiniCssExtractPlugin.loader,
              {
                loader: 'css-loader',
                options: {
                  importLoaders: 1
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  plugins: [require('autoprefixer')]
                }
              }
            ]
          },
          {
            test: /\.less$/,
            use: [
              MiniCssExtractPlugin.loader,
              {
                loader: 'css-loader',
                options: {
                  importLoaders: 2
                }
              },
              {
                loader: 'postcss-loader',
                options: {
                  plugins: [require('autoprefixer')]
                }
              },
              {
                loader: 'less-loader',
                options: { modifyVars, javascriptEnabled: true }
              }
            ]
          },
          {
            test: /\.(woff|woff2|eot|ttf|otf)$/,
            use: {
              loader: 'file-loader',
              options: {
                name: 'static/font/[name].[hash:8].[ext]'
              }
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new LodashModuleReplacementPlugin(),
    new webpack.DefinePlugin({
      VERSION: JSON.stringify(VERSION),
      ENABLE_REDUX_LOG: false,
      DEPLOY_TIME: DEPLOY_TIME,
      ENVIRONMENT: JSON.stringify('prod')
    }),
    new CopyWebpackPlugin([pathConfig.appPublic]),
    new HtmlWebpackPlugin({
      template: pathConfig.appHtml,
      chunks: ['app']
    }),
    new HtmlWebpackPlugin({
      template: pathConfig.appHtml,
      filename: 'updating.html',
      chunks: ['updating']
    }),
    new webpack.optimize.MinChunkSizePlugin({
      minChunkSize: 20000
    }),
    new MiniCssExtractPlugin({
      filename: 'static/css/[name].[chunkhash:8].css',
      chunkFilename: 'static/css/[name].[chunkhash:8].chunk.css'
    }),
    new webpack.DllReferencePlugin({
      manifest: path.resolve(__dirname, 'vendor/vendor-manifest.json')
    }),
    new AddAssetHtmlPlugin([
      {
        filepath: require.resolve(path.resolve(__dirname, 'vendor/vendor.dll.js'))
      }
    ])
  ],
  performance: {
    hints: 'warning'
  }
};
